//
//  XFPictureView.m
//  SinaBlog_OC
//
//  Created by macs on 15/11/17.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

#import "XFPictureView.h"
#import "XFPictureCell.h"
#import "SDImageCache.h"
#define kPictureMargin 5
static NSString *pictureId = @"pictureId";

@interface XFPictureView () <UICollectionViewDataSource>

@property(nonatomic, weak) UILabel *countLable;

@end

@implementation XFPictureView

- (UILabel *)countLable {

  if (!_countLable) {
    UILabel *countLable = [[UILabel alloc] init];
    countLable.font = [UIFont systemFontOfSize:20];
    countLable.textColor = [UIColor redColor];
    //    countLable.center = self.center;
    [self addSubview:countLable];
    self.countLable = countLable;
  }
  return _countLable;
}

- (void)setPicUrls:(NSArray *)picUrls {
  _picUrls = picUrls;
  [self reloadData];

  CGSize size = [self calculateCollectionViewSize];
  [self mas_updateConstraints:^(MASConstraintMaker *make) {
    make.width.equalTo(@(size.width));
    make.height.equalTo(@(size.height));
  }];

    self.countLable.text = [NSString stringWithFormat:@"%ld", picUrls.count];
  self.backgroundColor =
      [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0)
                      green:((float)arc4random_uniform(256) / 255.0)
                       blue:((float)arc4random_uniform(256) / 255.0)
                      alpha:1.0];
}

- (CGSize)calculateCollectionViewSize {

  NSInteger count = self.picUrls.count;
  if (count == 0) {
    return CGSizeZero;
  }
  //列数
  int cols = 3;
  // collectionView 最大宽度
  CGFloat maxWidth = kScreenW - 2 * kStatueCellMargin;
  //单张图片最大宽度
  CGFloat picWidth = (maxWidth - 2 * kPictureMargin) / cols;
  UICollectionViewFlowLayout *layout =
      (UICollectionViewFlowLayout *)self.collectionViewLayout;
  layout.itemSize = CGSizeMake(picWidth, picWidth);
  if (count == 1) {
    CGSize size = CGSizeMake(150, 160);
    //获取数据
    NSString *urlKey = [self.picUrls.lastObject absoluteString];
    UIImage *image =
        [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:urlKey];
    size = image.size;
    layout.itemSize = size;
    return size;
  }
  if (count == 4) {
    CGFloat w = 2 * picWidth + kPictureMargin;
    return CGSizeMake(w, w);
  }

  //多张图片是 计算行数
  NSInteger rows = (count - 1) / cols;

  CGFloat hight = (rows + 1) * picWidth + rows * kPictureMargin;
  return CGSizeMake(maxWidth, hight);
}

// 1.重写 init 方法

- (instancetype)init {

  UICollectionViewFlowLayout *layout =
      [[UICollectionViewFlowLayout alloc] init];

  layout.minimumLineSpacing = kPictureMargin;
  layout.minimumInteritemSpacing = kPictureMargin;

  self.dataSource = self;
  [self registerClass:[XFPictureCell class]
      forCellWithReuseIdentifier:pictureId];

  if (self = [super initWithFrame:CGRectZero collectionViewLayout:layout]) {
    [self setupUI];
  }

  return self;
}

// 2. setupUI添加子控件
- (void)setupUI {
  [self addSubview:self.countLable];
  self.countLable.center = self.center;
  //  [self setupFrame];
}

// 3. setupFrame添加子控件约束
//- (void)setupFrame {
//  [self.countLable mas_makeConstraints:^(MASConstraintMaker *make) {
//
//    make.centerX.equalTo(self.mas_centerX);
//    make.centerY.equalTo(self.mas_centerY);
//  }];
//}

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {

  return self.picUrls.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {

  XFPictureCell *cell =
      [collectionView dequeueReusableCellWithReuseIdentifier:pictureId
                                                forIndexPath:indexPath];
  cell.pictUrl = self.picUrls[indexPath.item];
  return cell;
}

@end
